<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,initial-scale=1.0,width=device-width"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">

    <title>安监卫士登录界面</title>

    <link rel="stylesheet" type="text/css" href="css/api.css" />
    <link rel="stylesheet" type="text/css" href="css/boil.css" />
    <link rel="stylesheet" type="text/css" href="css/lib/iconfont/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="css/lib/bootstrap/bootstrap.min.css" />

    <style type="text/css">
        html, body{
            height: 100%;
        }

        body {
            background: #2F9AD0 url("image/login_bg.jpg") no-repeat 0 0 fixed;
        }

        .iconfont {
            font-size: 20px;
        }

        .div-top {
            height: 18%;
        }

        .div-logo {
            height: 60px;
            line-height: 60px;
            text-align: center;
            background: url("image/logo.png") no-repeat center center;
            background-size: 100px 56px ;
        }

        .div-logo-name {
            height: 80px;
            line-height: 80px;
            text-align: center;
            background: url("image/logo_name.png") no-repeat center center;
            background-size: 300px 49px ;
        }

        .div-set-ip {
            text-align: right;
        }
    </style>
</head>
<body>
    <div class="div-top"></div>
    <div class="div-logo"></div>
    <div class="div-logo-name"></div>

    <div class="container">
        <form class="form-horizontal" role="form">
            <div class="form-group input-group">
                <span class="input-group-addon">
                    <i class="iconfont icon-iconfontuser2"></i>
                </span>

                <input id="username" class="form-control font-size-18" type="text" placeholder="请输入账号">
            </div>

            <div class="form-group input-group">
                <span class="input-group-addon">
                    <i class="iconfont icon-mima"></i>
                </span>

                <input id="passwd" class="form-control font-size-18" type="password" placeholder="请输入密码">
            </div>

            <div class="form-group">
                <button id="login-btn" class="btn btn-block btn-lg btn-info" type="button">登录</button>
            </div>
        </form>

        <div class="div-set-ip">
            <a id="set-ip" class="text-white" style="text-decoration: none;" href="javascript:void(0);">设置服务器IP</a>
        </div>
    </div>

    <!-- [JS 脚本] -->
    <script type="text/javascript" src="script/api.js"></script>
    <script type="text/javascript" src="script/lib/jquery/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="script/lib/tether/tether.min.js"></script>
    <script type="text/javascript" src="script/lib/bootstrap/bootstrap.min.js"></script>
    <script type="text/javascript" src="script/boil.js"></script>

    <script type="text/javascript">
        apiready = function() {
            // 账号的 jquery 对象
            var $username = $("#username");
            // 密码的 jquery 对象
            var $passwd = $("#passwd");
            // UIListView 模块
            var uiListVIew = api.require("UIListView");
            // 服务器 IP 数组
            var serverIps = new Array();
            // UIListVIew 数据
            var uiListVIewData = new Array();
            // 是否显示 ListView 的标志：0-关闭，1-打开
            var uiListVIewFlag = 0;
            // 六盘水，账号：administrator，密码：lpssajj@boil
            // 钟山区煤矿，账号：5202010021，密码：000000
            // 水城县，账号：5202210006，密码：

            // 添加服务器 IP
            serverIps.push({
                countyNo : "5202",
                countyName : "六盘水市（陈勇）",
                serverIp : "http://168.168.168.65:8088/"
            });
            serverIps.push({
                countyNo : "5202",
                countyName : "六盘水市（欧阳神）",
                serverIp : "http://168.168.168.51:8088/"
            });
            serverIps.push({
                countyNo : "5202",
                countyName : "六盘水市（娄建勇）",
                serverIp : "http://168.168.168.156:8088/"
            });
            serverIps.push({
                countyNo : "5202",
                countyName : "六盘水市",
                serverIp : "http://111.121.223.244:8081/"
            });
            serverIps.push({
                countyNo : "520201",
                countyName : "钟山区",
                serverIp : "http://218.201.222.177:8081/"
            });
            serverIps.push({
                countyNo : "520203",
                countyName : "六枝特区",
                serverIp : "http://58.42.211.99:8081/"
            });
            serverIps.push({
                countyNo : "520221",
                countyName : "水城县",
                serverIp : "http://111.121.223.244:8082/"
            });
            serverIps.push({
                countyNo : "520222",
                countyName : "盘县",
                serverIp : "http://222.87.118.79:18080/"
            });

            for (var i = 0; i < serverIps.length; i++) {
                uiListVIewData.push({
                    title : serverIps[i].countyName,
                    subTitle : serverIps[i].serverIp
                });
            }

            // 清空偏好中的煤矿信息
            $boil.clearAllMineInfo();

            // 将账号和密码显示在输入框中
            $username.val($boil.getUsername());
            $passwd.val($boil.getPasswd());

            //----------[事件绑定]----------//
            //----------[退出程序]----------//
            // 点击返回键，退出程序（只支持 Android，不支持 IOS）
            api.addEventListener({
                name: "keyback"
            }, function (ret, err) {
                // 退出程序
                api.closeWidget({
                    id: api.appId,
                    silent: true
                });
            });

            // 关闭登录界面
            api.addEventListener({
                name: "closeLogin"
            }, function(ret, err) {
               api.closeWin();
            });
            //----------[/退出程序]----------//

            // 点击登录
            $("#login-btn").click(function() {
                var $this = $(this);
                var username = $username.val();
                var passwd = $passwd.val();

                uiListVIew.hide();

                if ($.isBlank($boil.getServerIp())) {
                    $boil.toast("请设置服务器IP");

                    return;
                }

                if ($.isBlank(username)) {
                    $boil.toast("请输入账号");

                    $username.focus();

                    return;
                }

                if ($.isBlank(passwd)) {
                    $boil.toast("请输入密码");

                    $passwd.focus();

                    return;
                }

                $this.attr("disabled", "disabled").html("正在登录……");

                api.ajax({
                    url: $boil.getServerIp() + "supervise/j_acegi_security_check.action",
                    method: "get",
                    timeout: 30,
                    dataType: "text",
                    data: {
                        values: {
                            j_username: username,
                            j_password: passwd
                        }
                    }
                }, function (ret, err) {
                    // 登录失败
                    if (err) {
                        $boil.toast(err.msg);

                        // 清空缓存
                        api.clearCache();

                        $this.removeAttr("disabled").html("登录");

                        // 登录验证
                    } else {
                        // 登陆成功
                        if ($.isBlank(ret)) {
                            getCurrentLoginInfo($this, username, passwd);
                        } else {
                            var result = $api.strToJson(ret);

                            if (result.success) {
                                getCurrentLoginInfo($this, username, passwd);
                            } else {
                                $boil.toast("用户名或密码错误");
                                $boil.clearLoginInfo();

                                // 清空缓存
                                api.clearCache();

                                $this.removeAttr("disabled").html("登录");
                            }
                        }
                    }
                });
            });

            // 点击设置服务器 IP
            $("#set-ip").click(function() {
                if ($("#login-btn").is(":disabled")) {
                    return;
                }

                if (uiListVIewFlag === 0) {
                    uiListVIewFlag = 1;

                    uiListVIew.open({
                        rect: {
                            x: 0,
                            y: (api.winHeight - 200),
                            h: 200
                        },
                        data: uiListVIewData,
                        styles: {
                            borderColor: "#2F9AD0",
                            item: {
                                bgColor: "#FFFFFF",
                                activeBgColor: "#F1F1F1",
                                height: 50,
                                titleSize: 14,
                                titleColor: "#2F9AD0",
                                subTitleSize: 12,
                                subTitleColor: "#2F9AD0"
                            }
                        }
                    }, function(ret, err) {
                        if (ret) {
                            if (ret.eventType == "clickContent") {
                                uiListVIew.hide();

                                $boil.setServerIp(serverIps[ret.index].serverIp);
                            }
                        } else {
                            uiListVIew.hide();

                            $boil.toast("选择服务器IP列表失败");
                        }
                    });
                } else {
                    uiListVIew.show();
                }
            });
            //----------[/事件绑定]----------//

            /**
             *
             * 获取当前登录信息。
             *
             * @param loginBtn 登陆按钮
             * @param username 账号
             * @param passwd 密码
             *
             */
            function getCurrentLoginInfo(loginBtn, username, passwd) {
                api.ajax({
                    url: $boil.getServerIp() + "supervise/supervise/safesecretary/getCurrentLoginInfo.action",
                    method: "get",
                    timeout: 30,
                    dataType: "json"
                }, function (ret, err) {
                    if (ret) {
                        if (ret.success && $.isArray(ret.results) && (ret.results.length > 0)) {
                            var loginInfo = ret.results[0];

                            uiListVIewFlag = 0;
                            uiListVIew.close();

                            // 保存登录标志，已登录
                            $boil.setLoginFlag("1");
                            // 保存用户名
                            $boil.setUsername(username);
                            // 保存密码
                            $boil.setPasswd(passwd);

                            // 市级管理员登录
                            if ("4" == loginInfo.flag) {
                                $boil.setLoginCountyNo(loginInfo.currentDistrictCountyNo);
                                $boil.setCountyNo(loginInfo.currentDistrictCountyNo);

                                $boil.setLoginCountyName("六盘水市");
                                $boil.setCountyName("六盘水市")

                                $boil.openWin({
                                    name: "sj",
                                    url: "widget://html/sj.html"
                                });

                                // 县级管理员登录
                            } else if ("3" == loginInfo.flag) {
                                $boil.setLoginCountyNo(loginInfo.currentDistrictCountyNo);
                                $boil.setCountyNo(loginInfo.currentDistrictCountyNo);

                                if ("520201" == loginInfo.currentDistrictCountyNo) {
                                    $boil.setLoginCountyName("钟山区");
                                    $boil.setCountyName("钟山区")
                                } else if ("520203" == loginInfo.currentDistrictCountyNo) {
                                    $boil.setLoginCountyName("六枝特区");
                                    $boil.setCountyName("六枝特区")
                                } else if ("520221" == loginInfo.currentDistrictCountyNo) {
                                    $boil.setLoginCountyName("水城县");
                                    $boil.setCountyName("水城县")
                                } else if ("520222" == loginInfo.currentDistrictCountyNo) {
                                    $boil.setLoginCountyName("盘县");
                                    $boil.setCountyName("盘县")
                                }

                                $boil.openWin({
                                    name: "xj",
                                    url: "widget://html/xj.html"
                                });

                                // 集团公司管理员登录
                            } else if ("2" == loginInfo.flag) {
                                $boil.alert("集团公司暂不能登录！");

                                $boil.clearLoginInfo();
                                $boil.clearAllMineInfo();

                                // 清空缓存
                                api.clearCache();

                                loginBtn.removeAttr("disabled").html("登录");

                                // 企业管理员登录
                            } else if ("1" == loginInfo.flag) {
                                $boil.setLoginCountyNo(loginInfo.colliery_no.substring(0, 6));
                                $boil.setCurrentDistrictCountyNo(loginInfo.currentDistrictCountyNo);
                                $boil.setCountyNo(loginInfo.colliery_no.substring(0, 6));
                                $boil.setMineNo(loginInfo.colliery_no);
                                $boil.setOrgNo(loginInfo.mineno);

                                var mineNoSubstr = loginInfo.mineno.substring(0, 6);

                                if ("520201" == mineNoSubstr) {
                                    $boil.setLoginCountyName("钟山区");
                                    $boil.setCountyName("钟山区")
                                } else if ("520203" == mineNoSubstr) {
                                    $boil.setLoginCountyName("六枝特区");
                                    $boil.setCountyName("六枝特区")
                                } else if ("520221" == mineNoSubstr) {
                                    $boil.setLoginCountyName("水城县");
                                    $boil.setCountyName("水城县")
                                } else if ("520222" == mineNoSubstr) {
                                    $boil.setLoginCountyName("盘县");
                                    $boil.setCountyName("盘县")
                                }

                                $boil.openWin({
                                    name: "zk",
                                    url: "widget://html/zk.html"
                                });

                                // 其他
                            } else {
                                $boil.alert("用户类型错误，禁止登录！");

                                $boil.clearLoginInfo();
                                $boil.clearAllMineInfo();

                                // 清空缓存
                                api.clearCache();

                                loginBtn.removeAttr("disabled").html("登录");
                            }
                        } else {
                            $boil.toast("获取用户信息失败");

                            // 清空缓存
                            api.clearCache();

                            loginBtn.removeAttr("disabled").html("登录");
                        }
                    } else {
                        $boil.toast(err.msg);

                        // 清空偏好中的登录信息
                        $boil.clearLoginInfo();

                        // 清空缓存
                        api.clearCache();

                        loginBtn.removeAttr("disabled").html("登录");
                    }
                });
            }
        };
    </script>
    <!-- [/JS 脚本] -->
</body>
</html>